<template>
  <view class="header">
    <!-- 使用变量控制css类名 -->
    <view :class="[state === 0 ? 'active' : '']" @click="state = 0">
      进行中
    </view>
    <view :class="[state === 1 ? 'active' : '']" @click="state = 1">
      已结束
    </view>
    <view :class="[state === 2 ? 'active' : '']" @click="state = 2">
      我参与的
    </view>
  </view>
  <view>
    <up-card v-for="(act, index) in actList" :key="index" :title="act.name">
      <template #body>
        <view class="act-item" @click="toActInfo(act.id)">
          <image :src="base64ToBlobUrl(act.url)" class="act-img" />
          <view class="act-info">
            <view class="act-info-title"> {{ act.name }}开始啦 </view>
            <view class="act-info-body">
              <view class="act-info-body-item"> 时间：{{ act.time }} </view>
              <view class="act-info-body-sub-item">
                状态：{{ act.state }}
              </view>
              <view class="act-info-body-sub-item act-btn">
                报名链接：<view class="act-btn-item">点我</view>
              </view>
            </view>
          </view>
        </view>
      </template>
    </up-card>
  </view>
</template>

<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app'
import { ref, watch } from 'vue'
import type { actItem } from '@/types/activityPage'
import { activeList } from '@/api/home'
import { base64ToBlobUrl } from '@/common/img'

const state = ref(0)
const actList = ref<actItem[]>([])

const toActInfo = (id: number) => {
  uni.navigateTo({
    url: `/pages/activity/activityInfo?id=${id}`
  })
}

watch(state, async (newState) => {
  const res = await activeList({ state: newState })
  actList.value = res.data
})

onLoad(async () => {
  uni.setNavigationBarTitle({
    title: '社区活动'
  })
  const res = await activeList({ state: state.value })
  actList.value = res.data
})
</script>

<style lang="scss" scoped>
.header {
  width: 100%;
  height: auto;
  padding: 60rpx 20rpx;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  // 设置下边框阴影
  box-shadow: 0 4rpx 12rpx rgba(189, 187, 189, 0.8);
  border-radius: 10rpx;
  view {
    font-size: 36rpx;
    font-weight: 500;
    padding: 0 20rpx;
    color: #999;
  }
  .active {
    color: #fca363;
    font-size: 40rpx;
  }
}
.act-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  .act-img {
    width: 45%;
    height: 200rpx;
    border-radius: 8rpx;
  }
  .act-info {
    width: 53%;
    height: auto;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    .act-info-title {
      width: 100%;
      font-size: 34rpx;
      overflow: hidden;
      text-wrap: nowrap;
      text-overflow: ellipsis;
    }
    .act-info-body {
      width: 100%;
      color: #999;
      font-size: 26rpx;
    }
    .act-btn {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .act-btn-item {
        width: 30%;
        padding: 5rpx 10rpx;
        border: 1px solid #fca363;
        text-align: center;
        color: #fca363;
        border-radius: 50rpx;
        margin-left: 30rpx;
      }
    }
  }
}

// 深度选择器
::v-deep .u-card__head--left__title {
  font-size: 34rpx !important;
}
</style>
